﻿/*
 * BFind - A tool for finding football matches for trading on Betfair
 * Copyright 2009, 2010 Magnus Olsson <magnus72(at)gmail.com>
 * 
 * This file is part of BFind.
 * 
 * BFind is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BFind is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BFind.  If not, see <http://www.gnu.org/licenses/>
*/

using System;
using System.Collections.Generic;

namespace betfairfind.Rules
{
    class Fandango: BaseRule
    {

        double MAX_FAV_ODDS = 1.6;
        double HTCS_MIN_MATCHED = 500;
        double MIN_MATCHODDS_MATCHED = 500;

        public Fandango(Dictionary<string, string> config, List<MyMarket> inplaymarkets, Dictionary<long, MarketPricesCompressed> marketprices): base()
        {
        

            MarketsUsedInside = new string[] { "match odds", "half time score" };

            try
            {
                MAX_FAV_ODDS = Convert.ToDouble(config["Rule.FANDANGO.MAX_FAV_ODDS"]);


            }
            catch (Exception e)
            {
                Logger.LogError(e.ToString(), true, true);
               
            }

            string criteria = @"" + MAX_FAV_ODDS;
            //MAX_HOME_ODDS + "/" +
            //MIN_AWAY_ODDS + "/" +
            //MAX_AWAY_ODDS + "/" +
            //MIN_DRAW_ODDS + "/" +
            //MAX_DRAW_ODDS + "/" +
            //MIN_MATCHODDS_MATCHED + "/" +
            //MAX_OVER25_ODDS;

            Logger.Log("============================================================================", true, true);
            Logger.Log("Matches qualified for Fandango (" + criteria + ")", true, true);
            Logger.Log("----------------------------------------------------------------------------", true, true);

            if (!CheckRequiredMarkets()) { return; } 

            string[] games = FindSoccerGames.GetMatches();

            int selectionCount = 0;
            foreach (var game in games)
            {
                try
                {

                    MyMarket market_mo = FindSoccerGames.GetMarket(game, "match odds");
                    MarketPricesCompressed mpc_mo = marketprices[market_mo.marketId];
                    MyMarket market_htcs = FindSoccerGames.GetMarket(game, "half time score");
                    MarketPricesCompressed mpc_htcs = marketprices[market_htcs.marketId];
                    
                    double homeodds = mpc_mo.GetBestBackOdds(0);
                    double awayodds = mpc_mo.GetBestBackOdds(1);
                   // double drawodds = mpc_mo.GetBestLayOdds(2);
                    double matched = mpc_mo.TotalMatched;
                    double htcs_matched = mpc_htcs.TotalMatched;
                    double htcs10odds;

                    char _fav = 'H';
                    if (homeodds > awayodds)  _fav = 'A';

                    if ((_fav == 'A' && awayodds <= MAX_FAV_ODDS) && matched >= MIN_MATCHODDS_MATCHED && htcs_matched >= HTCS_MIN_MATCHED)
                    {
                        htcs10odds = mpc_htcs.GetBestBackOdds(6);
                        Logger.Log(market_mo.marketTime + ", " + BF.convertPathToGame(game) + ", home: " + homeodds + ", away: " + awayodds + ", 0-1: " + htcs10odds, true, true);
                        selectionCount++;
                    }

                    if ((_fav == 'H' && homeodds <= MAX_FAV_ODDS) && matched >= MIN_MATCHODDS_MATCHED && htcs_matched >= HTCS_MIN_MATCHED)
                    {
                        htcs10odds = mpc_htcs.GetBestBackOdds(3);
                        Logger.Log(market_mo.marketTime + ", " + BF.convertPathToGame(game) + ", home: " + homeodds + ", away: " + awayodds + ", 1-0: " + htcs10odds, true, true);
                        selectionCount++;
                    }

 
                }
                catch (Exception  ex)
                {

                    Logger.LogError("Error in game " + game + Environment.NewLine + ex.ToString(), true, false);

                }

            }
            if (selectionCount == 0)
            {
                Logger.Log("No matches", true, true);
            }
        }
    
    }
}
